package tt.dz.mvc.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import tt.dz.entity.base.JsonCommon;
import tt.dz.service.base.FileUploadService;
/**
 * 文件上传服务
 * @author huanghao
 *
 */
@Controller
@RequestMapping("/fileApi")
public class FileUploadAction extends BaseAction
{
	@Autowired
	@Qualifier("localFileService")
	//暂时采用本地上传策略，但是如果是多台服务器,很可能文件不存在，所有需要定时同步文件夹内容
	private FileUploadService fileUploadServiceImpl;
	
	@RequestMapping(value="upload",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> sysFileUpload(@RequestParam(value="file") MultipartFile file,
			HttpServletRequest request,HttpServletResponse response)
	{
		String localPath = request.getServletContext().getRealPath("/");
		String targetDir = "resource";
		String requestPath = new StringBuilder().append(request.getScheme()).append("://").
				append(request.getServerName()).append(":").append(request.getServerPort()).
				append(request.getContextPath()).append("/").toString();
		String fileFuns = request.getParameter("file_functions");
		String entity = request.getParameter("entity");
		String fileId  = request.getParameter("file_id");
		String entityId = request.getParameter("entity_id");
		return fileUploadServiceImpl.addFileUpload(file, fileFuns,entity,localPath, targetDir, requestPath,this.getLoginUserId(request),fileId,entityId);
	}
	
	@RequestMapping(value="oUpload",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> sysoFileUpload(@RequestParam(value="file") MultipartFile file,
			HttpServletRequest request,HttpServletResponse response)
	{
		String localPath = request.getServletContext().getRealPath("/");
		String targetDir = "resource";
		String requestPath = new StringBuilder().append(request.getScheme()).append("://").
				append(request.getServerName()).append(":").append(request.getServerPort()).
				append(request.getContextPath()).append("/").toString();
		String fileFuns = request.getParameter("file_functions");
		String entity = request.getParameter("entity");
		String fileId  = request.getParameter("file_id");
		String entityId = request.getParameter("entity_id");
		return fileUploadServiceImpl.addFileUpload(file, fileFuns,entity,localPath, targetDir, requestPath,this.getLoginUserId(request),fileId,entityId);
	}
	
}
